<?php

namespace App\Http\Controllers\Business;

use App\Models\Admin;
use App\Models\Agent;
use App\Models\Dictionary;
use App\Models\Manager;
use App\Models\Order;
use App\Models\Product;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Session;

class MemberController extends Controller
{
    public function index()
    {
        $info = Manager::find(Session::get('manager.id'),['id','name','company','wx_userinfo','invitation_code','type','phone']);
        if(!empty($info)){
            $apply = Order::where(['mid'=>$info->id,'status'=>0])->count();//正常
            $refused = Order::where(['mid'=>$info->id,'status'=>-1])->count();//退单
            $success = Order::where(['mid'=>$info->id,'status'=>1])->count();//放款
            $info['apply'] = $apply;
            $info['refused'] = $refused;
            $info['success'] = $success;
            return view('business.Member.index',compact('info'));
        }else{
            return redirect(url('product'))->with('message','暂无用户信息');
        }
    }
    /**
     * @param Request $request
     * @return array
     * ajax 异步获取数据
     */
    public function getOrder(Request $request)
    {
       $type = $request->type;
        if($type!==false)
        {
            $type == 2 ? $status = -1 : $status = $type;
            $list = Order::where(['mid'=>Session::get('manager.id'),'status'=>$status])
                ->select(['id','userinfo','mid','status','created_at','reason','confirm_loan','updated_at'])
                ->OrderBy('id','desc')
                ->paginate(10);
            if(!empty($list)){
                foreach($list as &$item)
                {
                    $product = Dictionary::find($item['userinfo']['apply_type']);
                    $item['product_name'] = $product ? mb_substr($product->name,0,2) : '';
                    if($item->status == 1){
                        $agent = Agent::where(['id'=>$item->confirm_loan['agent_id']])->first(['id','name']);
                        $item['agentName'] = $agent ? $agent->name : '';
                        $item['productName'] = $item->confirm_loan['product_name'];
                        $item['fee_value'] = $item->confirm_loan['fee_value'];
                        $category = Dictionary::where(['id'=>$item->confirm_loan['category_id_first']])->first(['id','name']);
                        $item['category'] = $category ? $category->name : '';
                    }elseif($item->status == -1){
                        $item['managerName'] = '管理员';
                        if($item->confirm_loan['admin_id']){
                            $manager = Admin::find($item->confirm_loan['admin_id'],['id','name']);
                            $item['managerName'] = $manager ? $manager->name : '管理员';
                        }
                    }
                }
                $this->set('data',$list);
            }else{
                $this->set('code',1);
                $this->set('message','暂无数据');
            }
        }else{
            $this->set('code',1);
            $this->set('message','参数错误');
        }
        return $this->ajaxResponse();
    }
}

